<template>
  <div class="content-share-chat">
    <div class="no-data" v-if="curChat === null">
      选择一位好友，开启愉快地聊天吧~
    </div>
    <div v-else>
      <div class="last-chat-time" v-if="curChat !== null">{{ curChat.chatTime || '-暂无聊天数据-' }}</div>
      <div class="chat-content-item" v-if="curChat !== null && curChat.curType === 'chat'">
        <el-avatar :size="37" shape="square" class="avatar">{{ curChat.name.substr(0, 1) }}</el-avatar>
        <div class="content-block">{{ curChat.chatContent }}</div>
      </div>
      <div class="chat-content-item" v-for="(item, i) in userChatContent" :key="i" style="text-align: right;">
        <div class="content-block right">{{ item.chatContent }}</div>
        <el-avatar :size="37" shape="square" class="avatar">{{ item.name.substr(0, 1) }}</el-avatar>
      </div>
    </div>
  </div>
</template>

<script>
export default {
  name: 'ContentShareChat',
  data() {
    return {
      curChat: null,
      userChatContent: [],
    }
  },
  watch: {
    curChat() {
      this.userChatContent = [];
    }
  },
  mounted() {
    this.$bus.$on('chatClick', (curChat) => {
      this.curChat = curChat;
    });
    this.$bus.$on('sendMessage', (message) => {
      this.userChatContent.push(message);
    });
  },
  beforeCreate() {
    this.$bus.$off('chatClick');
    this.$bus.$off('sendMessage');
  }
}
</script>

<style lang="less">
.content-share-chat {
  width: 100%;
  height: 100%;
  overflow: auto;

  .no-data {
    position: absolute;
    left: 50%;
    top: 50%;
    transform: translate(15%);
    font-size: 14px;
    color: #999;
  }

  .last-chat-time {
    width: 100%;
    height: 30px;
    line-height: 30px;
    text-align: center;
    font-size: 12px;
    color: #cccccc;
    font-weight: 500;
  }

  .chat-content-item {
    width: 100%;
    box-sizing: border-box;
    padding: 10px 15px;

    .avatar {
      &.el-avatar {
        background-color: #09aaff;
      }
    }

    .content-block {
      min-height: 37px;
      max-width: 250px;
      background: red;
      display: inline-block;
      vertical-align: top;
      box-sizing: border-box;
      padding: 8px;
      margin-left: 10px;
      border-radius: 4px;
      background: #bfe778;
      font-size: 14px;
      line-height: 21px;
      word-wrap: break-word;
      word-break: normal;

      &.right {
        margin-right: 10px
      }
    }
  }
}
</style>